package com.zl.freight.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.progresslayout.ProgressLayout;
import com.zl.freight.R;
import com.zl.freight.base.BaseActivity;
import com.zl.freight.mode.CarInformationEntity;
import com.zl.freight.mode.TopNewsBean;
import com.zl.freight.utils.API;
import com.zl.freight.utils.SoapCallback;
import com.zl.freight.utils.SoapUtils;
import com.zl.zlibrary.adapter.UniversalAdapter;
import com.zl.zlibrary.adapter.UniversalViewHolder;
import com.zl.zlibrary.utils.GsonUtils;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zhanglei
 *         广告列表页
 */
public class ADListActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.ad_listView)
    ListView adListView;
    @BindView(R.id.ad_trl)
    TwinklingRefreshLayout adTrl;

    private boolean isEdit = false;
    private List<CarInformationEntity> mList = new ArrayList<>();
    private UniversalAdapter<CarInformationEntity> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adlist);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initData() {
        getNews();
    }

    /**
     * 获取管理员发布的广告
     */
    private void getNews() {
        SoapUtils.Post(mActivity, API.GetAdminInfo, null, new SoapCallback() {
            @Override
            public void onError(String error) {
                showToast(error);
            }

            @Override
            public void onSuccess(String data) {
                try {
                    JSONArray array = new JSONArray(data);
                    for (int i = 0; i < array.length(); i++) {
                        CarInformationEntity carInformationEntity = GsonUtils.fromJson(array.optString(i), CarInformationEntity.class);
                        mList.add(carInformationEntity);
                    }
                    mAdapter.notifyDataSetChanged();
                } catch (Exception e) {

                }
            }
        });
    }

    private void initView() {
        tvTitleRight.setVisibility(View.VISIBLE);
        tvTitleRight.setText("编辑");

        mAdapter = new UniversalAdapter<CarInformationEntity>(mActivity, mList, R.layout.ad_item_layout) {
            @Override
            public void convert(UniversalViewHolder holder, int position, CarInformationEntity s) {
                holder.getView(R.id.linear_action).setVisibility(View.VISIBLE);
                View view = holder.getView(R.id.iv_zhi_ding);
                if (position == 0) {
                    view.setVisibility(View.VISIBLE);
                } else {
                    view.setVisibility(View.GONE);
                }

                holder.setText(R.id.ad_title, s.getInfoTitle());
                holder.setText(R.id.ad_content, s.getInfoTitle());
                holder.setText(R.id.ad_time, s.getUpdateAt());
            }
        };
        adListView.setAdapter(mAdapter);

        adTrl.setHeaderView(new ProgressLayout(mActivity));
    }

    @OnClick({R.id.iv_back, R.id.tv_title_right, R.id.fab_add_ad})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_title_right:
                edit();
                break;
            case R.id.fab_add_ad:
                add();
                break;
        }
    }

    private void add() {
        startActivity(new Intent(mActivity, ADActivity.class));
    }

    /**
     * 开启编辑
     */
    private void edit() {
        if (isEdit) {
            tvTitleRight.setText("编辑");
        } else {
            tvTitleRight.setText("取消编辑");
        }
        isEdit = !isEdit;
        mAdapter.notifyDataSetChanged();
    }
}
